﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */


#region using
using NetBay.Core.BusinessEntities;
using System.Collections.Generic;
using System;
using NetBay.Core.Log;
using System.Data;
#endregion


namespace Rio.Doc.Business.Repository.Translator
{
    public class ErpTranslator
    {

        /// <summary>
        /// Translate 
        /// </summary>
        /// <param name="P_oDbDataReader"></param>
        /// <returns></returns>
        public static Organization TranslateErpConnByName(IDataReader P_oDbDataReader)
        {
            var l_lstOrganization = new Organization();

            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        
                        var oneFolderType = new FolderType();
                        oneFolderType.OrganizationIdentifier = Convert.ToInt32(P_oDbDataReader["RE_ID"].ToString());
                        oneFolderType.Identifier = Convert.ToInt32(P_oDbDataReader["rp_td_id"].ToString());
                        oneFolderType.Name = P_oDbDataReader["rp_td_table"].ToString();

                        l_lstOrganization.Identifier = Convert.ToInt32(P_oDbDataReader["RE_ID"].ToString());
                        l_lstOrganization.Name = P_oDbDataReader["re_nom"].ToString();
                        l_lstOrganization.FolderTypesList.Add(oneFolderType);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }
            return l_lstOrganization;
        }


        /// <summary>
        /// Translate a bunch of Genesis DataReader to Organization object
        /// </summary>
        /// <param name="P_oDbDataReader">The dataReader that contains the rows</param>
        /// <returns>A list of Param Genesis Object</returns>
        public static List<Genesis> TranslateGenesis(IDataReader P_oDbDataReader)
        {
            var l_lstGenesis = new List<Genesis>();

            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        Genesis l_oGenesis = new Genesis();
                        //RP_TD_RE_ID,rp_td_id, recor_posit_index

                        l_oGenesis.IdentifierOrga = Convert.ToInt32(P_oDbDataReader["RE_ID"].ToString());
                        l_oGenesis.IdentifierTD = Convert.ToInt32(P_oDbDataReader["rp_td_id"].ToString());
                        l_oGenesis.IndexPosit = Convert.ToInt32(P_oDbDataReader["recor_index_pos"].ToString());
                        l_oGenesis.IdentifierIndex = Convert.ToInt32(P_oDbDataReader["RP_CH_NO"].ToString());
                        l_oGenesis.DescriptIndexCorrespondance = P_oDbDataReader["recor_description"].ToString();
                        l_oGenesis.NameTD = P_oDbDataReader["rp_td_table"].ToString();
                        l_lstGenesis.Add(l_oGenesis);
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_lstGenesis;
        }


        /// <summary>
        /// Table r_erp_correspondance
        /// </summary>
        /// <param name="P_oDbDataReader"></param>
        /// <returns></returns>
        public static string TranslateParamValeur(IDataReader P_oDbDataReader)
        {

            var l_sValue = "";

            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        l_sValue = P_oDbDataReader["repar_rio_valeur"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);
            }

            return l_sValue;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="P_oDbDataReader"></param>
        /// <returns></returns>
        public static bool TranslateReturnIndividu(IDataReader P_oDbDataReader)
        {

            bool l_bIndividuExist = false;
            string l_sValue = "";

            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        l_sValue = P_oDbDataReader[1].ToString();
                    }

                }
                if (l_sValue != "") l_bIndividuExist = true;

            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);

            }

            return l_bIndividuExist;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="P_oDbDataReader"></param>
        /// <returns></returns>
        public static bool TranslateReturnCourrierId(IDataReader P_oDbDataReader)
        {

            bool l_bCourrierIdExist = false;
            string l_sCourrierId = "";

            try
            {
                using (P_oDbDataReader)
                {
                    while (P_oDbDataReader.Read())
                    {
                        l_sCourrierId = P_oDbDataReader[0].ToString();
                    }

                }
                if (l_sCourrierId != "") l_bCourrierIdExist = true;

            }
            catch (Exception ex)
            {
                LogManager.Instance().LogError(NetBay.Core.Diagnostics.Method.GetMethodBaseName(NetBay.Core.Diagnostics.Method.Level.Father) + " EXCEPTION -> " + Environment.NewLine + ex);

            }

            return l_bCourrierIdExist;
        }


    }
}
